﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Web.utility.page;
using Ecreate.Model;
using Ecreate.Business;
namespace Ecreate.Web.pages
{
    public partial class customer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                this.Pager.CurrentIndex = 1;
                ProvinceCustomerService service = new ProvinceCustomerService();
                BindRepeater();
            }
            string pageNumberQS = Request.QueryString[Pager.QueryStringParameterName] ?? string.Empty;
            if (pageNumberQS != string.Empty && Convert.ToInt32(pageNumberQS) > 0)
            {
                Pager.CurrentIndex = Convert.ToInt32(pageNumberQS);
            }
            string areacode = Request.QueryString["areacode"];
            if (!String.IsNullOrEmpty(areacode))
            {
                this.hid_areacode.Value = areacode;
                BindRepeater(getWhere());
            }
        }
        private string getWhere()
        {
            string where = "";
            if(String.IsNullOrEmpty(this.hid_areacode.Value))
            {
                where="";
            }
            else
            {
                where = " and areacode like '{0}%'";
                where = string.Format(where, this.hid_areacode.Value);
            }
            return where;
        }


        protected void Pager_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
            this.Pager.CurrentIndex = currnetPageIndx;
            BindRepeater(getWhere());
        }

        private void BindRepeater(string where)
        {
            CustomerServiceDBPage service = new CustomerServiceDBPage(where);
            service.Pager.PageCurrent = this.Pager.CurrentIndex;
            service.Execute();
            this.Pager.ItemCount = service.Pager.RecordCount;
            this.rpt_customer.DataSource = service.List;
            this.rpt_customer.DataBind();
        }
        private void BindRepeater()
        {
            BindRepeater("");
        }

        public int getPageSize()
        {
            return Ecreate.Business.DBPageConst.PageSize;
        }
        public bool getEqual(object value)
        {

            if (this.hid_areacode.Value == value.ToString())
                return true;
            else
                return false;

        }

        protected void rpt_province_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {

            }
        }

        protected string getSqlWhere()
        {



            string where = " and areacode like '{0}%'";
            string province = this.hid_province.Value;
            string city = this.hid_city.Value;
            string country = this.hid_country.Value;




            if (province == "")
            {

                this.hid_province_name.Value = "";
            }
            if (city == "")
            {
                this.hid_city_name.Value = "";
            }
            if (country == "")
            {
                this.hid_country_name.Value = "";
            }




            string sql = "";
            if (province != "" && city == "" && country == "")
            {
                where = " and areacode like '{0}%'";
                where = string.Format(where, province);
                sql = where;

            }
            if (province != "" && city != ""&&country=="")
            {
                where = " and areacode like '{0}%'";
                where = string.Format(where, city);
                sql = where;
            }
            if (province != "" && city != "" && country != "")
            {
                where = " and areacode like '{0}%'";
                where = string.Format(where, country);
                sql = where;
            }
            return sql;
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {



            string blank = "  ";
            string SqlWhere = this.getSqlWhere();
            BindRepeater(SqlWhere);
            this.Label1.Text = this.hid_province_name.Value + blank + this.hid_city_name.Value + blank+this.hid_country_name.Value;
        }


    }
}
